package com.os.main;

import com.os.enums.Sex;
import com.os.mapper.FileMapper;
import com.os.mapper.RoleMapper;
import com.os.mapper.StudentMapper;
import com.os.mapper.UserMapper;
import com.os.po.Role;
import com.os.po.Student;
import com.os.po.TFile;
import com.os.po.User;
import com.os.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @Author Matt
 * @Date 2022-06-23
 * @Desc:
 * @Version V1.0
 */
public class Main {

    public static void main(String[] args) throws IOException {
        SqlSession sqlSession = null;
        FileInputStream fileInputStream = null;
        try {
            sqlSession = SqlSessionFactoryUtil.openSqlSession();
            RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

            Role role = new Role();
            //role.setId(999);
            role.setRoleName("Matt");
            role.setNote("testNote");

            //roleMapper.insertRole(role);
            //roleMapper.deleteRole(999);

            Role roleResult = null;

            //roleResult = roleMapper.getRole(99);
            //roleResult = roleMapper.findRole(role.getRoleName());
            //System.out.println(roleResult.toString());

            User user = new User();
            user.setId(4L);
            user.setUser_name("Matt");
            user.setCnname("宋传磊");
            user.setMobile("18251303287");
            user.setSex(Sex.MALE);
            user.setEmail("songchuanlei@hotmail.com");
            user.setNote("test EnumOrdinalTypeHandler！！！");
            user.setBirthday(new Date());

            //userMapper.insertUser(user);

            User user2 = userMapper.getUser(4L);

            System.out.println(user2.getSex());

            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
            Student student = studentMapper.getStudent(1);
            System.out.println(student);

            System.out.println("------测试文件写入和读取-------");

            File file = new File("E:\\婚宴宾客名单.xls");
            fileInputStream = new FileInputStream(file);
            byte[] bytes = new byte[(int) file.length()];
            fileInputStream.read(bytes);

            TFile tFile = new TFile();
            tFile.setFile(bytes);
            FileMapper fileMapper = sqlSession.getMapper(FileMapper.class);
            fileMapper.insertFile(tFile);
            System.err.println(tFile.getId());

            tFile = fileMapper.getFile(1);
            System.err.println(tFile.getFile().length);

            List<Role> roleList = roleMapper.findRoleByName("a", new RowBounds(0, 2));
            for(Role role1 : roleList){
                System.err.println(role1.toString());
            }

            sqlSession.commit();
        }catch (Exception e){
            System.err.println(e.getMessage());
            sqlSession.rollback();
        }finally{
            if(sqlSession != null){
                sqlSession.close();
            }
            if(fileInputStream != null){
                fileInputStream.close();
            }
        }
    }
}
